
import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Font;

import java.sql.Connection;

import java.sql.SQLException;



import net.sf.jasperreports.engine.JRAbstractChartCustomizer;

import net.sf.jasperreports.engine.JRChart;



import org.apache.log4j.Logger;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.CategoryAxis;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.block.BlockBorder;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.renderer.category.StackedBarRenderer;

import org.jfree.chart.title.LegendTitle;

import org.jfree.chart.title.TextTitle;

import org.jfree.chart.title.Title;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.ui.HorizontalAlignment;

import org.jfree.ui.RectangleEdge;

import org.jfree.ui.RectangleInsets;



import com.db.misportal.util.ConnectionPool;

import com.db.report.jasper.custom.consumptionreport.dataset.ODBCDataset;



public class ConsumptionChartCustomizer extends JRAbstractChartCustomizer {

 /** Default Constanst */

 protected static final java.awt.Font HEADER_FONT = new java.awt.Font(

   "Arial", Font.BOLD + Font.ITALIC, 14);



 protected static final Color HEADER_COLOR = new Color(0x00, 0x00, 0xDE);



 protected static final float BAR_WIDTH = 0.03f;



 protected static final java.awt.Font TITLE_FONT = new java.awt.Font(

   "Arial", Font.BOLD, 8);



 protected static final Color TITLE_COLOR = new Color(0x00, 0x00, 128);



 protected static final java.awt.Font TICKET_FONT = new java.awt.Font(

   "Arial", Font.PLAIN, 8);



 protected static final java.awt.Font LEGEND_FONT = new java.awt.Font(

   "Arial", Font.PLAIN, 8);



 protected static final java.awt.Color GRID_COLOR = new java.awt.Color(0, 0,

   0);



 private final static String[] DEFAULT_MONTH_ABBREVIATION = { "J", "F", "M",

   "A", " M", " J", " J ", " A", " S", "O", "N", "D" };



 protected static final String DEFAULT_TITLE = "Default Title";



 protected static final String DEFAULT_SUB_TITLE = null;



 protected static final String DEFAULT_DOMAIN_AXIS_LABEL = null;



 protected static final String DEFAULT_RANGE_AXIS_LABEL = null;



 protected static final RectangleEdge DEFAULT_LEGEND_POSITION = RectangleEdge.TOP;



 protected JFreeChart chart;



 private Title subTitle = null;



 private boolean hasSubTitle = false;



 private boolean hasLegend = false;



 protected static final RectangleEdge DEFAULT_SUBTITLE_POSITION = RectangleEdge.BOTTOM;



 protected static final HorizontalAlignment DEFAULT_SUBTITLE_HORIZONTAL_ALIGNMENT = HorizontalAlignment.LEFT;



 public void customize(JFreeChart chart, JRChart jrChart) {



  _log.info("customize jFreeChart ...");

  this.chart = chart;



  /* Set Chart Title */

  try {

   TextTitle title = chart.getTitle();

   title.setHorizontalAlignment(HorizontalAlignment.LEFT);

  } catch (NullPointerException e) {

  }



  /* Chart Properties */

  chart.setBackgroundPaint(Color.white);

  chart.setBorderPaint(new Color(192, 192, 192));

  chart.setBorderVisible(true);

  chart.setBorderStroke(new BasicStroke(0.001f));

  chart.setPadding(new RectangleInsets(1, 1, 1, 1)); // top,left,bottom,right



  /* Legend Properties */

  hasLegend = true;

  try {

   LegendTitle legend = chart.getLegend();

   legend.setItemFont(LEGEND_FONT);

   legend.setBorder(BlockBorder.NONE);

   legend.setPosition(DEFAULT_LEGEND_POSITION);

  } catch (NullPointerException e) {

   hasLegend = false;

  }



  /* Chart subtitle */

  try {

   int subTitleIndex = 0;

   if (hasLegend) {

    subTitleIndex = 1;

   } else {

    subTitleIndex = 0;

   }



   hasSubTitle = true;

   subTitle = chart.getSubtitle(subTitleIndex);

   setSubTitlePosition(DEFAULT_SUBTITLE_POSITION);

   setSubTitleHorizontalAlignment(DEFAULT_SUBTITLE_HORIZONTAL_ALIGNMENT);

  } catch (IllegalArgumentException e) {

   hasSubTitle = false;

   subTitle = null;

   _log.debug("This chart has no subtitle.");

  }



  /* Axis: Ticket Label + Line */

  try {

   CategoryPlot plot = chart.getCategoryPlot();

   NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();

   numberAxis.setTickLabelFont(TICKET_FONT);

   numberAxis.setTickLabelPaint(Color.black);

   numberAxis.setAxisLinePaint(Color.black);

   numberAxis.setAxisLineStroke(new BasicStroke(0.001f));

   numberAxis.setTickMarkPaint(GRID_COLOR);

   numberAxis.setTickMarkStroke(new BasicStroke(0.001f));

   numberAxis.setTickMarkOutsideLength(3f);



   CategoryAxis categoryAxis = (CategoryAxis) plot.getDomainAxis();

   categoryAxis.setTickLabelFont(TICKET_FONT);

   categoryAxis.setTickLabelPaint(Color.black);

   categoryAxis.setAxisLinePaint(Color.black);

   categoryAxis.setAxisLineStroke(new BasicStroke(0.001f));

   categoryAxis.setAxisLineVisible(false);

   categoryAxis.setTickMarkPaint(GRID_COLOR);

   categoryAxis.setTickMarkStroke(new BasicStroke(0.001f));

   categoryAxis.setTickMarkOutsideLength(3f);

   categoryAxis.setTickMarkInsideLength(1f);



   // plot.setBackgroundPaint(Color.gray);

   /* Grid */

   // plot.setRangeGridlinesVisible(false);

   plot.setRangeGridlinePaint(GRID_COLOR);

   plot.setRangeGridlineStroke(new BasicStroke(0.001f));

   plot.clearRangeMarkers();



   /* Outline */

   plot.setOutlinePaint(null);



   /* Unit */

   numberAxis

     .setStandardTickUnits(NumberAxis.createIntegerTickUnits());



   StackedBarRenderer renderer = (StackedBarRenderer) plot

     .getRenderer();

   renderer.setMaximumBarWidth(BAR_WIDTH);

   renderer.setDrawBarOutline(true);

   renderer.setOutlinePaint(Color.black);

   renderer.setOutlineStroke(new BasicStroke(0.00001f));



   /* Series Color - dont need it any more - we set color in iReport */

   // renderer.setSeriesPaint(0, new Color(153, 153, 255));

   // renderer.setSeriesPaint(1, new Color(153, 51, 102));

   // renderer.setSeriesPaint(2, new Color(255, 255, 204));

   // renderer.setSeriesPaint(3, new Color(204, 255, 255));

  } catch (NullPointerException e) {

  }

 }



 /**

  * Set query with default month abbreviation

  * 

  * @param query

  */

 protected void setQuery(String query) {



  this.setQuery(query, DEFAULT_MONTH_ABBREVIATION);

 }



 /**

  * Set Chart Query : execute query and set plot dataset

  * 

  * @param query

  * @param month_abbreviation

  */

 protected void setQuery(String query, String[] month_abbreviation) {

  Connection conn = null;

  DefaultCategoryDataset dataset = null;



  try {

   conn = ConnectionPool.getConnection();

   dataset = new ODBCDataset(conn, query);

  } catch (SQLException e) {

   e.printStackTrace();

  } catch (com.db.report.jasper.custom.common.dataset.NullDatasetException e) {

   _log.debug("Empty dataset...Hide chart");

   this.hide();



  } catch (ClassNotFoundException e) {

   e.printStackTrace();

  }



  CategoryPlot plot = chart.getCategoryPlot();

  plot.setDataset(dataset);

 }



 /**

  * Set chart title

  * 

  * @param strTitle

  */

 public void setTitle(String strTitle) {

  TextTitle title = chart.getTitle();



  if (title == null) {

   chart.setTitle(strTitle);

  } else {

   title.setText(strTitle);

  }

 }



 /**

  * Set Legend Position

  * 

  * @param position

  */

 public void setLegendPosition(RectangleEdge position) {

  try {

   LegendTitle legend = chart.getLegend();

   legend.setPosition(position);

  } catch (NullPointerException e) {

  }

 }



 /**

  * Make chart invisible

  * 

  */

 public void hide() {

  CategoryPlot plot = chart.getCategoryPlot();

  NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();

  numberAxis.setVisible(false);

  CategoryAxis categoryAxis = (CategoryAxis) plot.getDomainAxis();

  categoryAxis.setVisible(false);

  plot.setRangeGridlinesVisible(false);

 }



 /**

  * Set Subtitle Position

  * 

  * @param position

  */

 public void setSubTitlePosition(RectangleEdge position) {

  if (hasSubTitle && subTitle != null) {

   subTitle.setPosition(position);

  }

 }



 /**

  * Set Subtitle text

  * 

  * @param text

  */

 public void setSubTitleText(String text) {

  if (hasSubTitle && subTitle != null) {

   ((TextTitle) subTitle).setText(text);

  }

 }



 /**

  * Set Subtitle Horizontal Aligment (LEFT,RIGHT,CENTER)

  * 

  * @param align

  */

 public void setSubTitleHorizontalAlignment(HorizontalAlignment align) {



  if (hasSubTitle && subTitle != null) {

   subTitle.setHorizontalAlignment(align);

  }

 }



 /** Logger */

 static Logger _log = Logger.getLogger(ConsumptionChartCustomizer.class);

}